--[[
   @brief    2-factor authentication
   @author   senpingtian
   @version  1.0
   @date     2019-07-16
--]]

login_auth_verify_control = {
    
    init = function()
        base_utility.log("[login_auth_verify_control] init.");
        ierd_tgp.tpf_ui.Require("wegame_login\\auth_verify\\model.lua");
        ierd_tgp.tpf_ui.Require("wegame_login\\auth_verify\\view.lua");

        comm_center.reg_service("i_service_get_twofa_info", 
                                "login_auth_verify_control.on_get_auth_info");

        comm_center.reg_service("i_service_notify_twofa_result", 
                                "login_auth_verify_control.on_notify_result");

    end;

    on_show_window = function()
        login_auth_verify_view:on_show_dialog();
    end;

    on_wnd_pos_changed = function()
        login_auth_verify_view:on_pos_changed();
    end;

    on_close_window = function()
        login_auth_verify_view:on_close();
        if true == login_auth_verify_model.verify_result then
            comm_center.call_service("Srv_RequestReLogin", { acc_type = 6 });
        else
            local param = {
                err_code = 7610424;
            };
            TLoginCtrl.OnLoginFail(0, base_utility.table_to_json(param));
        end
    end;

    on_get_auth_info = function(param)
        local info = {
            code = login_auth_verify_model.code_2fa;
            mach_id = login_auth_verify_model.machine_id;
        };
        return info;
    end;

    delay_close_window_handler = function(userdata)
        base_utility.remove_interval("login_auth_verify_control.delay_close_window_handler");
        login_auth_verify_control.on_close_window();
    end;

    delay_close_window = function(timeout)
        if nil == timeout or 0 == timeout then
            timeout = 1000;
        end
        base_utility.add_interval("login_auth_verify_control.delay_close_window_handler", timeout);
    end;

    on_notify_result = function(param)
        if nil == param then
            return;
        end

        local param_tbl = {};
        if type(param) == 'string' then
            param_tbl = base_utility.json_to_table(param);
        elseif type(param) == 'table' then
            param_tbl = param;
        end

        local param_str = base_utility.table_to_json(param);
        base_utility.log("[login_auth_verify_control]notify result".. param_str, true);

        if 'success' == param_tbl.result then
            login_auth_verify_model.verify_result = true;
            login_auth_verify_control.delay_close_window(500); -- close after 500ms when success
        elseif 'cancel' == param_tbl.result then
            login_auth_verify_control.on_close_window();
        end
    end;
};


login_auth_verify_control.init();